package itheima_edu_info_manager.com.itheima.edu.info.manager.controller;

import itheima_edu_info_manager.com.itheima.edu.info.manager.domain.Teacher;
import itheima_edu_info_manager.com.itheima.edu.info.manager.service.TeacherService;

import java.util.Scanner;

public class TeacherController {
    Scanner sc = new Scanner(System.in);
    private TeacherService teacherService = new TeacherService();
    //创建老师管理系统菜单
    public void start() {
        studentLoop:
        while (true) {
            System.out.println("--------欢迎来到 <老师> 管理系统--------");
            System.out.println("请输入您的选择: 1.添加老师  2.删除老师  3.修改老师  4.查看老师  5.退出");
            String choice = sc.next();
            switch (choice) {
                case "1":
                    //System.out.println("添加老师");
                    addTeacher();
                    break;
                case "2":
                    //System.out.println("删除老师");
                    deleteTeacherById();
                    break;
                case "3":
                    //System.out.println("修改老师");
                    upDateTeacherById();
                    break;
                case "4":
                    //System.out.println("查看老师");
                    findAllTeacher();
                    break;
                case "5":
                    System.out.println("感谢使用老师管理系统");
                    break studentLoop;
                default:
                    System.out.println("输入的选择有误，请重新输入！");
                    break;
            }
        }
    }

    //修改老师
    public void upDateTeacherById() {
        String id;
        id = putTeacherId(true);
        Teacher teacher = getTeacher(id);

        teacherService.upDateTeacherById(id,teacher);
        System.out.println("修改成功");
    }

    //判断id是否存在
    public String putTeacherId(boolean flag) {
        String id;
        while (true){
            System.out.println("请输入id：");
            id = sc.next();
            boolean exists = teacherService.isExists(id);
            if (exists == flag){
                break;
            }else {
                System.out.println("输入的id无效，请重新输入!");
            }
        }
        return id;
    }

    //将接收到的数据，进行封装
    public Teacher getTeacher(String id) {
        System.out.println("请输入老师姓名");
        String name = sc.next();
        System.out.println("请输入老师年龄");
        String age = sc.next();
        System.out.println("请输入老师生日");
        String brithday = sc.next();

        return new Teacher(id, name, age, brithday);
    }

    //删除老师
    public void deleteTeacherById() {
        Teacher[] teacher = teacherService.findAllTeacher();

        int index = -1;

        if (teacher == null){
            System.out.println("无信息，请先添加后再操作");
            return;
        }else {
            System.out.println("请输入你需要删除的id");
            String id = sc.next();
            for (int i = 0; i < teacher.length; i++) {
                Teacher te = teacher[i];
                if (te.getId().equals(id)){
                    index = i;
                    break;
                }
            }
            if (index == -1){
                System.out.println("输入的id无效，请查询后重新输入");
                return;
            }else {
                    Teacher te = teacher[index];
                    System.out.println("确认是否需要删除" + te.getId() + "的老师信息吗？y or n");
                    System.out.println("学号\t\t姓名\t年龄\t生日");
                    System.out.println(te.getId() + "\t" + te.getName() + "\t" + te.getAge() + "\t\t" + te.getBirthday());
                    String choice = sc.next();
                    if (choice.equals("y")){
                        teacherService.deleteTeacherById(id);
                        System.out.println("删除成功！");
                        return;
                    }else {
                        System.out.println("手别抖。。");
                        return;
                    }
            }
        }
    }

    //查询老师
    public void findAllTeacher() {
        Teacher[] teachers = teacherService.findAllTeacher();
        if (teachers == null) {
            System.out.println("查无信息，请添加后再查询");
        }else {
            System.out.println("学号\t\t姓名\t年龄\t生日");
            for (int i = 0; i < teachers.length; i++) {
                Teacher te = teachers[i];
                if (te != null) {
                    System.out.println(te.getId() + "\t" + te.getName() + "\t" + te.getAge() + "\t\t" + te.getBirthday());
                }
            }
        }
    }

    //添加老师操作
    public void addTeacher() {
        String id;
        id = putTeacherId(false);
        Teacher teacher = getTeacher(id);

        boolean result = teacherService.addTeacher(teacher);
        if (result) {
            System.out.println("添加成功");
        } else {
            System.out.println("添加失败");
        }
    }
}
